<?php

include_once(APPPATH.'models/baseObject_model.php');

class Admin_user_model extends BaseObject_model {
	var $dbName = 'baby_mcenter';
	var $tableName = 'sys_users';
	var $primary_key = 'userId';

	var $defaultCols = 'username,realname,status,super,createtime,lastlogin';
	var $defaultOrder = array('userId'=>'asc');

	var $columns = array(
		'userId'=>array('label'=>'用户ID' , 'width'=>50 ,  'primary'=>true , 'isfilter'=>false),
		'username'=>array('label'=>'用户名' , 'width'=>125),
		'realname'=>array('label'=>'姓名' , 'width'=>125),
		'password'=>array('label'=>'密码' , 'width'=>125, 'isfilter'=>false),
		'status'=>array('label'=>'启用状态' , 'width'=>100),
		'createtime'=>array('label'=>'创建时间' , 'width'=>150 , 'type'=>'date'),
		'mtime'=>array('label'=>'最后修改时间' , 'width'=>150 , 'type'=>'date'),
		'lastlogin'=>array('label'=>'最后登录时间' , 'width'=>150 , 'type'=>'date'),
		'super'=>array('label'=>'超级管理员' , 'width'=>100),
		'disabled'=>array('label'=>'废除' , 'width'=>50 , 'type'=>'bool' , 'isfilter'=>false)
	);

	function __construct() {
		parent::__construct();
	}
	function findUserById($userId){
		$db = $this->load->database($this->dbName, true);
		$db->select('*');
		$db->from('sys_users');
		$db->where('userId', $userId);
		$db->where('disabled', 'false');
		$query = $db->get();
		$result = $query->row_array();
		$query->free_result();
		return $result;
	}
	function findGroupIdByUserId($userId){
		
		$user = $this->findUserById($userId);
		
		$db = $this->load->database($this->dbName, true);
		$db->select('groupId');
		$db->from('sys_user_group');
		
		if($user['super'] === 'false'){
			$db->where('userid', $userId);
		}
		
		$query = $db->get();
		$result = $query->result_array();
		$query->free_result();
		if(empty($result) || count($result) < 1){
			return -1;
		}
		$groupIds = array();
		foreach($result as $idx=>$group ){
			$groupIds[$idx] =$group['groupId'];
		}
		return $groupIds;
	}
	function findUserByName($username){
		$db = $this->load->database($this->dbName, true);
		$db->select('*');
		$db->from('sys_users');
		$db->where('username', $username);
		$db->where('disabled', 'false');
		$query = $db->get();
		$result = $query->row_array();
		$query->free_result();
		return $result;
	}
	
	function changePassword($userId, $password){
		$data = array();
		$data['password'] = md5($password);
		$data['mtime'] = date("Y-m-d H:i:s");
		$this->update($userId , $data);
	}
	
	function updateLoginTime($userId){
		$data = array();
		$data['mtime'] = date("Y-m-d H:i:s");
		$data['lastlogin'] = date("Y-m-d H:i:s");
		$this->update($userId , $data);
	}
	
	function editUser($user, $groupIds){
		$db = $this->load->database($this->dbName, true);
		$userId = $user['userId'];
		//删除role与operation的关联关系
		$db->delete('sys_user_group' , array('userid'=>$userId));
		$data = array();
		$data['realname'] = $user['realname'];
		$data['mobile'] = $user['mobile'];
		$data['status'] = $user['status'];
		$data['mtime'] = date("Y-m-d H:i:s");
		$this->update($userId , $data);
		if(is_array($groupIds)){
			//更新关联关系		
			foreach($groupIds as $groupId){
				$data = array('userId'=>$userId , 'groupId'=>$groupId);
				$db->insert('sys_user_group' , $data);
			}
		}
	}
	
	function addUser($user, $groupIds){
		$db = $this->load->database($this->dbName, true);
		$user['password'] = md5($user['password']);
		$user['username'] = trim($user['username']);
		$data['createtime'] = date("Y-m-d H:i:s");
		$userId = $this->insert($user);
		if(is_array($groupIds)){
			//更新关联关系		
			foreach($groupIds as $groupId){
				$data = array('userId'=>$userId , 'groupId'=>$groupId);
				$db->insert('sys_user_group' , $data);
			}
		}
		return true;
	}
	
	/*
	 * 验证账号
	 */
	function validate($username, $password){
		$db = $this->load->database($this->dbName, true);
		$db->select('*');
		$db->from('sys_users');
		$db->where('password', md5($password));
		$db->where('username', trim($username));
		$db->where('disabled', 'false');
		$db->where('status', 'true');
		$query = $db->get();
		$result = $query->row_array();
		$query->free_result();
		if(empty($result)){
			return false;
		}else{
			return $result;			
		}
	}
	
	/*
	 *验证权限 
	 */
	function checkAuth($auth, $user){
		$auth = trim($auth);
		$db = $this->load->database($this->dbName, true);
		$db->select('*');
		$db->from('sys_users u');
		$db->join('sys_user_group ug', 'ug.userId = u.userId');
		$db->join('sys_group_role gr' , 'ug.groupId = gr.groupId');
		$db->join('sys_role_operation ro' , 'ro.roleId = gr.roleId');
		$db->join('sys_operations o' , 'ro.opId = o.opId');
		$db->where('u.disabled', 'false');
		$db->where('o.disabled', 'false');
		$db->where('o.operation', $auth);
		$db->where('u.userId', $user['userId']);
		$query = $db->get();
		$result = $query->row_array();
		$query->free_result();
		if(empty($result)){
			return false;
		}
		return true;
	}
}
/* End of file admin_user_model.php */
/* Location: ./application/models/admin/admin_user_model.php */